package com.kb.design.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kb.design.dto.DesignProjectDTO;
import com.kb.design.entity.DesignProgress;
import com.kb.design.entity.DesignProject;
import com.kb.design.mapper.postgreSQL.DesignProgressMapper;
import com.kb.design.mapper.postgreSQL.ProjectMapper;
import com.kb.design.service.DesignProgressService;
import com.kb.design.service.ResponsiblePersonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
@DS("slave-1")
public class DesignProgressServiceImpl extends ServiceImpl<DesignProgressMapper, DesignProgress> implements DesignProgressService {
    @Resource
    private DesignProgressMapper designProgressMapper;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private ResponsiblePersonService responsiblePersonService;
    @Override
    public List<DesignProgress> queryProgressList(DesignProgress designProgress) {
        LambdaQueryWrapper<DesignProgress> wrapper = Wrappers.lambdaQuery(DesignProgress.class).eq(DesignProgress::getProjectId, designProgress.getProjectId())
                .eq(DesignProgress::getDelFlag, 0);
        if (Objects.equals(designProgress.getOrderDir(), "desc")){
            wrapper.orderByDesc(DesignProgress::getCreateTime);
        }else {
            wrapper.orderByAsc(DesignProgress::getCreateTime);
        }
        List<DesignProgress> list = designProgressMapper.selectList(wrapper);
        // 手动添加序号
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSerialNumber(i + 1);
        }
        return list;
    }


    @Override
    @DSTransactional
    public void insert(DesignProgress designProgress) {
        DesignProject project = new DesignProject();
        BeanUtil.copyProperties(designProgress, project,"id");
        if (designProgress.getDescribed() != null){
            project.setDescribed(designProgress.getDescribed());
        }
        if (designProgress.getId() == null){
            //修改项目表数据
            project.setDesignStatus(designProgress.getDesignStatus());
            project.setCompletionRate(designProgress.getCompletionRate());
            project.setId(designProgress.getProjectId());
            designProgressMapper.insert(designProgress);
        }else {
            //修改项目表数据
            project.setDesignStatus(designProgress.getDesignStatus());
            project.setCompletionRate(designProgress.getCompletionRate());
            project.setId(designProgress.getProjectId());
            projectMapper.updateById(project);
            designProgress.setUpdateTime(LocalDateTime.now());
            designProgressMapper.updateById(designProgress);
        }
        projectMapper.updateById(project);
        //如果进度到 100  直接进行下一步 到检查
        if (Objects.nonNull(designProgress.getCompletionRate()) & designProgress.getCompletionRate() == 100){
            DesignProjectDTO projectDTO = new DesignProjectDTO();
            BeanUtil.copyProperties(designProgress, projectDTO);
            responsiblePersonService.projectCompleted(projectDTO);
        }
    }

    @Override
    public boolean del(DesignProgress designProgress) {
        // 自定义软删除逻辑
        designProgress.setDelFlag(1); // 标记为已删除
        designProgress.setUpdateTime(LocalDateTime.now());
        this.updateById(designProgress);
        //查询最大的项目进度
        LambdaQueryWrapper<DesignProgress> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DesignProgress::getProjectId, designProgress.getProjectId())
                .eq(DesignProgress::getDelFlag, 0).eq(DesignProgress::getProjectId, designProgress.getProjectId())
                .orderByDesc(DesignProgress::getCompletionRate)
                .last("limit 1");
        DesignProgress progress = designProgressMapper.selectOne(queryWrapper);
        DesignProject designProject = new DesignProject();
        if (progress != null){
            designProject.setDescribed(progress.getDescribed());
            designProject.setCompletionRate(progress.getCompletionRate());
        }
        projectMapper.update(new DesignProject(),new UpdateWrapper<DesignProject>().lambda().eq(DesignProject::getId, designProgress.getProjectId())
                .set(DesignProject::getCompletionRate, designProject.getCompletionRate()).set(DesignProject::getDescribed, designProject.getDescribed()));
        return true;
    }
}
